<template>
  <div class="dept-container">
    <div class="search-box">
      <el-form size="mini" ref="searchFormRef" :model="searchForm" inline label-suffix="：">
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="searchForm.deptName"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="dataStatus">
          <el-select v-model="searchForm.dataStatus" >
            <el-option :label="item.label" :value="item.value" :key="index"  v-for="(item,index) in dataStatusOptions"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handlerSearch">搜索</el-button>
          <el-button @click="resetSearchForm" >重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="btn-wrapper">
      <el-button size="mini" @click="addDept(undefined)">新建</el-button>
    </div>
    <div class="table-wrapper">
      <el-table size="mini" border v-loading="loading" row-key="id" stripe :data="tableData">
        <el-table-column type="index"></el-table-column>
        <el-table-column label="部门名称" prop="name"></el-table-column>
        <el-table-column label="排序" prop="sort"></el-table-column>
        <el-table-column label="状态" >
          <template v-slot="{row}">
            <div>{{translateDictCode(row.dataStatus,dataStatusOptions)}}</div>
          </template>
        </el-table-column>
        <el-table-column  label="创建时间" prop="createTime"></el-table-column>
        <el-table-column  label="修改时间" prop="updateTime"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="{row}">
            <el-button type="text" size="mini" @click="addDept(row.id)">新增</el-button>
            <el-button type="text" size="mini" @click="editDept(row.id)">修改</el-button>
            <el-button type="text" size="mini" @click="delDept(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-wrapper">
    </div>
    <el-dialog :model-value="visible" destroy-on-close width="400px" v-model="visible" title="新增/修改" @close="dialogClose">
      <div>
        <el-form size="mini" :rules="rules" :model="deptForm" ref="deptFormRef" label-suffix="：" label-width="100px">
          <el-form-item label="父部门">
            <tree-select v-model="deptForm.parentId"  :clearable="false" :options="treeSelectOptions"></tree-select>
          </el-form-item>
          <el-form-item label="部门名称" prop="name">
            <el-input type="text" v-model="deptForm.name"></el-input>
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input-number style="width: 100%" v-model="deptForm.sort" :min="0" controls-position="right"></el-input-number>
          </el-form-item>
          <el-form-item label="状态" prop="dataStatus">
            <el-select v-model="deptForm.dataStatus" :key="selectKey" :model-value="deptForm.dataStatus" style="width: 100%">
              <el-option :label="item.label" :value="item.value" :key="index"  v-for="(item,index) in dataStatusOptions"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div  style="text-align: right">
          <el-button size="mini" round type="primary" @click="submitDeptForm($event)">确定</el-button>
          <el-button size="mini" round type="danger" @click="visible = false">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import {defineComponent, nextTick, reactive,ref, watch,toRefs} from "vue";
import useSearch from "@/views/system/dept/hooks/useSearch";
import useTable from "@/views/system/dept/hooks/useTable";
import useForm from "@/views/system/dept/hooks/useForm";
import useTranslateDictCode, {DictData} from "@/hooks/useTranslateDictCode/useTranslateDictCode";
import useDataStatusOptions from "@/hooks/useDataStatusOptions/useDataStatusOptions";
import TreeSelect from 'vue-treeselect-next'
import "vue-treeselect-next/dist/vue-treeselect.css"
export default defineComponent({
  name:'Dept',
  components:{TreeSelect},
  setup() {
    const {searchForm,handlerSearch,resetSearchForm} = useSearch()
    const { table,getDataList,loading,treeSelectOptions} = useTable()
    const {deptForm,rules,visible,addDept,editDept,delDept,dialogClose,submitDeptForm} = useForm()
    getDataList()
    const selectKey = ref(Date.now())
    watch(visible,(val)=>{
      if (val){
        nextTick(()=>{
          selectKey.value = Date.now()
        })
      }
    })
    return {
      searchForm,
      treeSelectOptions,
      loading,
      ...toRefs(table),
      handlerSearch,
      resetSearchForm,
      visible,
      addDept,
      editDept,
      delDept,
      deptForm,
      rules,
      dialogClose,
      submitDeptForm,
      ...useTranslateDictCode(),
      ...useDataStatusOptions(),
      selectKey
    }
  },
});
</script>
<style lang="scss" scoped>
.dept-container {
  padding: 10px;
}
</style>
